from tensorflow.keras import layers, models, losses, optimizers, metrics
import numpy as np

x_data = np.array([[1, 2],
          [2, 3],
          [3, 1],
          [4, 3],
          [5, 3],
          [6, 2]])
y_data = np.array([[0],
          [0],
          [0],
          [1],
          [1],
          [1]])

model = models.Sequential()
model.add(layers.Dense(1, input_dim=(2), activation='sigmoid'))

#配置模型：定义Adam优化器, binary_crossentropy的损失,binary_accuracy的评估指标
model.compile(optimizer=optimizers.Adam(0.01), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy])

#训练返回数据history包含: 就是model.compile里面定义的内容: loss, metrics
history = model.fit(x_data, y_data, epochs=500)

# 绘制损失曲线，准确率曲线
loss = history.history['loss']
acc = history.history['binary_accuracy']

import matplotlib.pyplot as plt
plt.plot(loss)
plt.show()
plt.plot(acc)
plt.show()

# 3,4
print(model.predict([[3, 4]]))   #预测值，sigmoid输出
print(model.predict_classes([[3,4]]))  #预测类别